<?php
	function usespan($text,$style="")
	{
		if ($style=="")
			return $text;
		else
			return '<span class="'.$style.'">'.$text.'</span>';
	}

class paging
{	 
	function page($total,$perpage,$pagenumber="",$linkformat="",$linkstyle="")
	{	 		
		$out="";
		if($total>$perpage){		
				if($linkformat==""){
					$linkformat=$_SERVER['PHP_SELF']; $linkformat.="?";
				}
				else{
					if(substr_count($linkformat,"?")>0) $linkformat.="&"; else $linkformat.="?";
				}
			
				if($pagenumber=="")
		 			if(isset($_REQUEST['page']))
		  				$pagenumber=$_REQUEST['page'];
		 			else
		 				$pagenumber=1;
						
		 		$lastpage=(int)($total/$perpage);				
		 		if($total%$perpage!=0)
		 			$lastpage+=1;
					
		 		if($linkstyle!="") $linkstyle="class=\"".$linkstyle."\"";
				
		 		if($pagenumber>=2)
		 			$out.="<a href=\"$linkformat"."page=".($pagenumber-1)."\" $linkstyle >Previous</a>&nbsp;&nbsp;";

				for($i=0;$i<(int)($total/$perpage);$i++){
					if($pagenumber==($i+1))
						$out.=($i+1)."&nbsp;&nbsp;";
					else
						$out.="<a href=\"$linkformat"."page=".($i+1)."\" $linkstyle  >".($i+1)."</a>&nbsp;&nbsp;";
				}
			
				if(($total%$perpage)!=0){
					if($pagenumber==$lastpage)
						$out.=($i+1)."&nbsp;&nbsp;";
					else
						$out.="<a href=\"$linkformat"."page=$lastpage\">".$lastpage." $linkstyle</a>&nbsp;&nbsp;";
				}
			
	 			if($pagenumber<$lastpage)
	 				$out.="<a href=\"$linkformat"."page=".($pagenumber+1)."\" $linkstyle >Next</a>";
	  	}
		return $out;
	}	
	
	function pagemailing($total,$perpage,$pagecount,$pagenumber="",$linkformat="",$linkstyle="",$textstyle="",$numstyle="")
	{	 		
		$out="";
		if($total>$perpage){		
				if($linkformat==""){
					$linkformat=$_SERVER['PHP_SELF']."?";
				}
				else{
					if(substr_count($linkformat,"?")>0) 	$linkformat.="&"; 
					else 									$linkformat.="?";
				}
				if($linkstyle!="") $linkstyle=' class="'.$linkstyle.'"';
				
				if($pagenumber=="")
		 			if(isset($_REQUEST['page']))
		  				$pagenumber=$_REQUEST['page'];
		 			else
		 				$pagenumber=1;
											
				//new
				if($total%$perpage==0)
					$totalpages = (int)($total/$perpage);
				else
					$totalpages = (int)($total/$perpage+1);
			
				if($pagenumber=="")
		 			if(isset($_REQUEST['page']))
		  				$pagenumber=$_REQUEST['page'];
		 			else
		 				$pagenumber=1;

				//$out = usespan("Current page:",$textstyle)."&nbsp;".usespan("".$pagenumber,$numstyle)."&nbsp;&nbsp;&nbsp;".usespan("Total pages:",$textstyle)."&nbsp;".usespan("".$totalpages,$numstyle)."<br>";
				 
				$start=1;
				$end=1;
				$out1="";
				for($i=1; $i<=$totalpages; $i++)
				{         
					if(($i>((int)(($pagenumber-1)/$pagecount))* $pagecount) && ($i<=((int)(($pagenumber-1)/$pagecount+1))* $pagecount))
					{
							if($start==1) $start=$i;
							if($i==$pagenumber)
									$out1 .= usespan("".$i,$numstyle)."&nbsp;&nbsp;";
							else
									$out1 .= '<a href="'.$linkformat.'page='.$i.'">'.$i.'</a>&nbsp;&nbsp;';
							$end=$i;
					}
				}
				
				$out.= usespan("Go to page:",$textstyle)."&nbsp;&nbsp;";
				
				if($pagenumber > $pagecount)
					$out .= '<a href="'.$linkformat.'page='.($start-1).'">'.'<<'.'</a>&nbsp;&nbsp;';
					
				$out.=$out1;
				
				if (($end + 1) > $totalpages)
				{
					if(((($pagenumber-1)/$pagecount+1)*$pagecount) < $totalpages)
							$out .= '<a href="'.$linkformat.'page='.($end+1).'">'.'>>'.'</a>&nbsp;&nbsp;';
				}
				else
				{
					if ($pagenumber < $totalpages)
							$out .= '<a href="'.$linkformat.'page='.($end+1).'">'.'>>'.'</a>&nbsp;&nbsp;';
				}		
			//end new					
	  	}
		return $out;
	}		
	
	function pagenew($total,$perpage,$pagecount,$pagenumber="",$linkformat="",$linkstyle="",$textstyle="",$numstyle="")
	/*
		$total: total row count of data
		$perpage: number row of 1 page
		$pagecount: Number page will show 
		$pagenumber: Current page
		$linkformat: Link go to page 
		$linkstyle: Style of link
		$textstyle: Style of text ex: Go To page, current page
		$numstyle: Style of number current page or number of total page (ex: curent page is 4, or number total page : 15)
	*/
	{	 		
		
		$out="";
		if($total>$perpage){		
				if($linkformat==""){
					$linkformat=$_SERVER['PHP_SELF']."?";
				}
				else{
					if(substr_count($linkformat,"?")>0) 	$linkformat.="&"; 
					else 									$linkformat.="?";
				}
				if($linkstyle!="") $linkstyle=' class="'.$linkstyle.'"';
				
				if($pagenumber=="")
		 			if(isset($_REQUEST['page']))
		  				$pagenumber=$_REQUEST['page'];
		 			else
		 				$pagenumber=1;
											
				//new
				if($total%$perpage==0)
					$totalpages = (int)($total/$perpage);
				else
					$totalpages = (int)($total/$perpage+1);
			
				if($pagenumber=="")
		 			if(isset($_REQUEST['page']))
		  				$pagenumber=$_REQUEST['page'];
		 			else
		 				$pagenumber=1;

				//$out = usespan("Current page:",$textstyle)."&nbsp;".usespan("".$pagenumber,$numstyle)."&nbsp;&nbsp;&nbsp;".usespan("Total pages:",$textstyle)."&nbsp;".usespan("".$totalpages,$numstyle)."&nbsp;&nbsp;<br>";
				 
				$start=1;
				$end=1;
				$out1="";
				for($i=1; $i<=$totalpages; $i++)
				{         
					if(($i>((int)(($pagenumber-1)/$pagecount))* $pagecount) && ($i<=((int)(($pagenumber-1)/$pagecount+1))* $pagecount))
					{
							if($start==1) $start=$i;
							if($i==$pagenumber)
									$out1 .= usespan("".$i,$numstyle)."&nbsp;&nbsp;";
							else
									$out1 .= '<a href="'.$linkformat.'curpg='.$i.'">'.$i.'</a>&nbsp;&nbsp;';
							$end=$i;
					}
				}
				
				$out.= usespan("Go to page:",$textstyle)."&nbsp;&nbsp;";
				
				if($pagenumber > $pagecount)
					$out .= '<a href="'.$linkformat.'curpg='.($start-1).'">'.'<<'.'</a>&nbsp;&nbsp;';
					
				$out.=$out1;
				
				if (($end + 1) > $totalpages)
				{
					if(((($pagenumber-1)/$pagecount+1)*$pagecount) < $totalpages)
							$out .= '<a href="'.$linkformat.'curpg='.($end+1).'">'.'>>'.'</a>&nbsp;&nbsp;';
				}
				else
				{
					if ($pagenumber < $totalpages)
							$out .= '<a href="'.$linkformat.'curpg='.($end+1).'">'.'>>'.'</a>&nbsp;&nbsp;';
				}		
			//end new					
	  	}
		$out = usespan($out,$linkstyle);
		return $out;
	}	
}
?>